using System.Xml.Linq;

namespace Tequila.Phone.Sharing.Twitpic
{
    /// <summary>
    /// XDoc extensions.
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// Gets element value.
        /// </summary>
        /// <param name="root">Root <see cref="XElement">element</see>.</param>
        /// <param name="name">Searching element name.</param>
        /// <returns></returns>
        public static string GetElementValue(this XElement root, string name)
        {
            var value = string.Empty;
            if (root == null)
            {
                return value;
            }

            var element = root.Element(name);

            return element == null ? value : element.Value;
        }

        /// <summary>
        /// Gets element attribute value.
        /// </summary>
        /// <param name="root">Root <see cref="XElement">element</see>.</param>
        /// <param name="name">Searching element attribute name.</param>
        /// <returns></returns>
        public static string GetAttributeValue(this XElement root, string name)
        {
            var value = string.Empty;
            if (root == null)
            {
                return value;
            }

            var attribute = root.Attribute(name);

            return attribute == null ? value : attribute.Value;
        }
    }
}